#include "FeatProxyModel.h"
#include "ItemModel.h"



namespace
{
QString FEAT_GROUP_ID = ".Feat";
}



FeatProxyModel::FeatProxyModel(QObject *parent) :
    AbstractItemProxyModel(parent)
{
}


int FeatProxyModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return (Field_LAST - Field_FIRST + 1);
}



QVariant FeatProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    Q_UNUSED(orientation);

    if (role != Qt::DisplayRole) return QVariant();
    switch (section)
    {
    case NameField: return tr("Feat");
    }

    return QVariant();
}


void FeatProxyModel::remap()
{
    ItemModel* model = dynamic_cast<ItemModel*>(sourceModel());
    if (!model) return;

    QModelIndex groupSource = model->index(0,AbstractItem::IdData, QModelIndex());
    while (groupSource.isValid())
    {
        if (groupSource.data(Qt::EditRole).toString().contains(FEAT_GROUP_ID)) break;

        groupSource = groupSource.sibling(groupSource.row()+1, AbstractItem::IdData);
    }

    if (!groupSource.isValid()) return;

    QModelIndex featSource = groupSource.child(0,AbstractItem::NameData);
    while (featSource.isValid())
    {
        addMapping(featSource, createIndex(featSource.row(), NameField, featSource.internalPointer()));

        featSource = featSource.sibling(featSource.row()+1, AbstractItem::NameData);
    }
}
